const { resolve } = require('./utils');
const fs = require('fs');
const rootPath = process.cwd();
const customConfigPath = `${rootPath}/jest.overwrite.js`;
const customConfig = fs.existsSync(customConfigPath) ? require(customConfigPath) || {} : {};
const formatJestAlign = (align) => {
  let _align = {};
  for (let key in align) {
    let value = align[key].replace(rootPath, '');
    if (key === '@') {
      _align['^@$'] = '<rootDir>/$1';
    } else {
      _align[`^${key}(.*)$`] = `<rootDir>${value}/$1`;
    }
  }
  return _align;
};

const syfCollectCoverageFrom = ['src/*/*.{jsx,tsx}', '!**/node_modules/**', '!src/index.tsx'];

const customCollectCoverageFrom = customConfig.collectCoverageFrom
  ? customConfig.collectCoverageFrom
  : [];

module.exports = Object.assign(
  {
    verbose: true,
    transform: {
      '\\.[jt]sx?$': 'babel-jest',
      '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
    },
    moduleDirectories: [`${rootPath}/node_modules`, 'node_modules'],
    rootDir: rootPath,
    setupFiles: [`${process.cwd()}/node_modules/@seeyon/syf/scripts/jest-setup.js`],
    moduleNameMapper: formatJestAlign(resolve.alias),
  },
  customConfig,
  {
    collectCoverageFrom: [...new Set([...syfCollectCoverageFrom, ...customCollectCoverageFrom])],
  },
);
